//
//  HWStrategyListView.swift
//  HWanGame
//
//  Created by Apple on 2024/8/13.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class HWStrategyListView: UITableViewCell {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(14.font_semiBold).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(11.font_medium).end()
    private let joinButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.createImageFrom(color: "#F2F3F5".toColor), state: .selected)
        .bgImage(UIImage.createImageFrom(color: "#2E3033".toColor))
        .text("加入").text("已加入", state: .selected)
        .color(.white).color("#C8C9CC".toColor, state: .selected)
        .corner(13*widthScale).image("Forum_join").font(12.font_semiBold)
        .image("Luntan_joined", state: .selected).end()
    private let contentImage = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    
    var joinStrategy: ((_ shequID: String) -> Void)?
    
    var model: HWGameCommunityAllListModel? {
        didSet{
            guard let md = model else { return }
            iconImageView.sd_setImage(with: URL(string: md.icon))
            contentImage.sd_setImage(with: URL(string: md.bgimg))
            nameLabel.text = md.name
            subLabel.text = md.guanzhu_num + "用户已加入"
            joinButton.isSelected = md.is_guanzhu
        }
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                iconImageView
                nameLabel
                subLabel
                joinButton
                contentImage
            }
        }
                
        backView.bottom(12*widthScale).leading(12*widthScale).trailing(12*widthScale).top(12*widthScale)
        iconImageView.leading(12*widthScale).top(16*widthScale).size(40*widthScale)
        nameLabel.leading(60*widthScale).trailing(95*widthScale).top(16*widthScale)
        subLabel.leading(60*widthScale).trailing(95*widthScale).top(40*widthScale)
        joinButton.trailing(20*widthScale).top(23*widthScale).width(66*widthScale).height(26*widthScale)
        contentImage.leading(12*widthScale).trailing(12*widthScale).top(68*widthScale).height(175*widthScale)
        joinButton.addTarget(self, action: #selector(clickJoinButton), for: .touchUpInside)
    }
    
    @objc private func clickJoinButton(){
        joinStrategy?(model?.id ?? "")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


}

//MARK: 论坛列表topView
class HWStrategyListTopView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private let moreButton = c_make_ex(UIButton.self)
        .image("Circle_more").end()
    
    var sourceData: [HWGameCommunityAllListModel]? {
        didSet{
            if sourceData?.count ?? 0 > 5 {
                moreButton.isHidden = false
            }else{
                moreButton.isHidden = true
            }
            collectionView.reloadData()
        }
    }
    var toStrategyDetailVC: ((_ shequID: String) -> Void)?
    var moreBlock: ((_ datas: [HWGameCommunityAllListModel]) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        
        addSubview(collectionView)
        addSubview(moreButton)
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 55*widthScale, height: 75*widthScale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuAttentionStrategyCell.self, forCellWithReuseIdentifier: HWWuAttentionStrategyCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        moreButton.addTarget(self, action: #selector(clickMoreButton), for: .touchUpInside)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        moreButton.snp.makeConstraints { make in
            make.trailing.top.bottom.equalToSuperview()
            make.width.equalTo(75*widthScale)
        }
    }
    
    @objc private func clickMoreButton(){
        guard let dataArr = sourceData else { return }
        moreBlock?(dataArr)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return sourceData?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuAttentionStrategyCell.description(), for: indexPath) as? HWWuAttentionStrategyCell else { return UICollectionViewCell() }
        cell.model = sourceData?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        var models = [HWGameCommunityAllListModel]()
        sourceData?.forEach { model in
            let itemModel = model
            itemModel.isSel = false
            models.append(itemModel)
        }
        models[indexPath.row].isSel = true
        sourceData = models
        toStrategyDetailVC?(models[indexPath.row].id)
        
        collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


//MARK: 已关注论坛cell
class HWWuAttentionStrategyCell: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8*widthScale).end()
    private let selView = c_make_ex(UIView.self)
        .bgColor(.clear).corner(8*widthScale).border("#FF7700", width: 1).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(11.font_medium).alignment(.center).end()
    
    var model: HWGameCommunityAllListModel? {
        didSet{
            guard let md = model else { return }
            if md.isLocal {
                nameLabel.textColor = "#FF7700".toColor
                iconImageView.image = md.icon.toImage
            }else{
                nameLabel.textColor = "#7B7D80".toColor
                iconImageView.sd_setImage(with: URL(string: md.icon))
            }
            nameLabel.text = md.name
            selView.isHidden = !md.isSel
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            selView
            iconImageView
            nameLabel
        }
        
        selView.top(0).leading(0).trailing(0).size(55*widthScale)
        iconImageView.leading(3*widthScale).trailing(3*widthScale).top(3*widthScale).size(48*widthScale)
        nameLabel.leading(0).trailing(0)
        nameLabel.Top == iconImageView.Bottom + 4*widthScale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
